//SPDX-FileCopyrightText: Copyright 2022-2024 深圳市同心圆网络有限公司
//SPDX-License-Identifier: GPL-3.0-only

package project_recycle_api_serv

import (
	"context"

	"atomgit.com/openlinksaas-org/api-server/dao/bulletin_dao"
	"atomgit.com/openlinksaas-org/api-server/dao/idea_dao"
	"atomgit.com/openlinksaas-org/api-server/dao/issue_dao"
	"atomgit.com/openlinksaas-org/api-server/dao/project_entry_dao"
	"atomgit.com/openlinksaas-org/api-server/dao/requirement_dao"
	"atomgit.com/openlinksaas-org/api-server/dao/testcase_dao"
	"atomgit.com/openlinksaas-org/proto-gen-go.git/project_recycle_api"
)

type ProjectRecycleApiImpl struct {
	project_recycle_api.UnimplementedProjectRecycleApiServer
}

func (apiImpl *ProjectRecycleApiImpl) getTitle(ctx context.Context, projectId, recycleItemId string, recycleItemType project_recycle_api.RECYCLE_ITEM_TYPE) (string, error) {
	if recycleItemType == project_recycle_api.RECYCLE_ITEM_TYPE_RECYCLE_ITEM_IDEA {
		item, err := idea_dao.IdeaRecycleDao.Get(ctx, projectId, recycleItemId)
		if err != nil {
			return "", err
		}
		return item.BasicInfo.Title, nil
	} else if recycleItemType == project_recycle_api.RECYCLE_ITEM_TYPE_RECYCLE_ITEM_REQUIREMENT {
		item, err := requirement_dao.RequirementRecycleDao.Get(ctx, projectId, recycleItemId)
		if err != nil {
			return "", err
		}
		return item.BaseInfo.Title, nil
	} else if recycleItemType == project_recycle_api.RECYCLE_ITEM_TYPE_RECYCLE_ITEM_TASK || recycleItemType == project_recycle_api.RECYCLE_ITEM_TYPE_RECYCLE_ITEM_BUG {
		item, err := issue_dao.IssueRecycleDao.Get(ctx, projectId, recycleItemId)
		if err != nil {
			return "", err
		}
		return item.BasicInfo.Title, nil
	} else if recycleItemType == project_recycle_api.RECYCLE_ITEM_TYPE_RECYCLE_ITEM_TESTCASE {
		item, err := testcase_dao.CaseRecycleDao.Get(ctx, recycleItemId, projectId)
		if err != nil {
			return "", err
		}
		return item.Title, nil
	} else if recycleItemType == project_recycle_api.RECYCLE_ITEM_TYPE_RECYCLE_ITEM_BULLETIN {
		item, err := bulletin_dao.BulletinRecycleDao.Get(ctx, recycleItemId, projectId)
		if err != nil {
			return "", err
		}
		return item.Title, nil
	} else {
		item, err := project_entry_dao.EntryRecycleDao.Get(ctx, recycleItemId, projectId)
		if err != nil {
			return "", err
		}
		return item.EntryTitle, nil
	}
}
